package cn.zz.veggie.persistence.repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollectionUtil;
import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.exception.classes.FatalErrorException;
import cn.zz.comm.util.CollUtil;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.veggie.model.domain.Admin;
import cn.zz.veggie.model.query.AdminQuery;
import cn.zz.veggie.persistence.dao.AdminMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 管理员表(Admin)repo
 *
 * @author zhongq
 * @since 2024-05-13 20:42:21
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class AdminRepository {

	private final AdminMapper adminMapper;

	public Admin findById(Integer id) {
		Objects.requireNonNull(id);
		return adminMapper.selectById(id);
	}

	public Map<Integer, Admin> findByIds(Collection<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return Collections.emptyMap();
		}
		final List<Admin> records = adminMapper.selectBatchIds(ids);
		return CollectorUtil.btmByKeyFun(records, Admin::getId);
	}

	public List<Admin> findByPage(AdminQuery query, PageQuery page) {
		final LambdaQueryWrapper<Admin> queryWrapper = query.toQueryWrapper();
		final Page<Admin> adminPage = query.toPage(page);
		final List<Admin> admins = adminMapper.selectList(adminPage, queryWrapper);
		page.setTotal(((int) adminPage.getTotal()));
		return admins;
	}

	public Admin findOne(AdminQuery query) {
		final LambdaQueryWrapper<Admin> queryWrapper = query.toQueryWrapper();
		final Page<Admin> page = query.toPage(PageQuery.first().setSearchCount(false));
		return CollUtil.getFirst(adminMapper.selectList(page, queryWrapper));
	}

	public void save(Admin admin) {
		Objects.requireNonNull(admin);
		if (admin.getId() == null) {
			// insert
			final int effect = adminMapper.insert(admin);
			if (effect != 1) {
				throw new FatalErrorException("created admin fail ");
			}
			log.info("created admin for {}", admin.getId());
		} else {
			// update
			final int effect = adminMapper.updateById(admin);
			if (effect != 1) {
				throw new FatalErrorException("update admin fail ");
			}
			log.info("updated admin for {}", admin.getId());
		}
	}

	public void deleteById(Integer id) {
		Objects.requireNonNull(id);
		final int effect = adminMapper.deleteById(id);
		if (effect != 1) {
			log.error("delete admin fail for {}", id);
			throw new FatalErrorException("delete admin fail ");
		}
		log.info("delete admin for {}", id);
	}

	public void deleteByIds(Collection<Integer> ids) {
		if (CollUtil.isEmpty(ids)) {
			return;
		}
		final int effect = adminMapper.deleteByIds(ids);
		log.info("delete admin for {}", effect);
	}

	public Admin findByMobile(String mobile) {
		Objects.requireNonNull(mobile);
		return null;
	}
}